//
//  CZBaseStatusCell.swift
//  czbkiosweibo5
//
//  Created by zhangping on 15/10/17.
//  Copyright © 2015年 zhangping. All rights reserved.
//

import UIKit

class CZStatusCell: UITableViewCell {
    
    // MARK: - 属性
    /// cell子控件间距
    let statusCellMargin: CGFloat = 8
    
    /// 配图宽度约束
    var pictureViewWidthCons: NSLayoutConstraint?
    
    /// 配图高度约束
    var pictureViewHeightCons: NSLayoutConstraint?
    
    /// 微博模型属性
    var status: CZStatus? {
        didSet {
//            print("父类属性: \(status!.id)")
            
            // 将模型赋值给topView
            topView.status = status
            
            // 设置微博内容
            contentLabel.text = status?.text
            
            // 将模型赋值给配图视图
            pictureView.status = status
            // 调用配图的calcViewSize获取计算好的宽高
            let size = pictureView.bounds.size
//            print(size)

            // 设置配图宽高
            pictureViewWidthCons?.constant = size.width
            pictureViewHeightCons?.constant = size.height
        }
    }
    
    // MARK: - 计算行高
    /**
    根据微博内容计算行高
    - parameter status: 微博内容
    - returns: 行高
    */
    func rowHeight(status: CZStatus) -> CGFloat {
        // 1.将status设置给当前cell对象.
        // 重新设置cell里面控件的内容
        self.status = status
        
        // 2.更新布局,会根据当前控件的内容重新计算约束,当使用AutoLayout后,不要手动去更改frame,bounds,center的值
        layoutIfNeeded()
        
        // 3.获取最大高度,获取bottomView的最大Y值
        return CGRectGetMaxY(bottomView.frame)
    }

    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 准备UI
        prepareUI()
    }
    
    // MARK: - 准备UI
    /**
    准备UI
    */
    func prepareUI() {
        // 添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        // 顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.height, height: 53))
        
        // 微博内容标签
        // 左边距离父控件8,右边距离父控件8,顶部距离父控件8
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellMargin, y: statusCellMargin))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.width - 2 * statusCellMargin))
        
        // 配图视图
//        // 在内容标签的左下位置,距离右边为8,宽高为0, 0
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 0, height: 0), offset: CGPoint(x: 0, y: statusCellMargin))
//        // 获取配图视图的宽度约束
//        pictureViewWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        
//        // 获取配图视图的高度约束
//        pictureViewHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        
        // 底部视图
        // 在微博内容下面8,左边和父控件对齐,宽度等于父控件宽度,高度44
        bottomView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: pictureView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: statusCellMargin))
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 44))
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        // contentView的底部和bottomView的底部重合
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    // MARK: - 懒加载控件
    /// 顶部视图
    private lazy var topView: CZStatusTopView = CZStatusTopView()
    
    /// 微博内容标签
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        // 设置文本多行
        label.numberOfLines = 0
        
        return label
    }()
    
    /// 微博配图视图
    lazy var pictureView: CZStatusPictureView = CZStatusPictureView()
    
    /// 底部视图
    lazy var bottomView: CZStatusBottomView = CZStatusBottomView()
}
